<?php
class Controller_Admin_Country extends Controller_Admin_Base {
		
	protected function actionSwitch() {
		switch ($this->arrRequest[2]) {
			case '': $this->actionCountriesList(); break;
 			case 'add': $this->actionAddCountry(); break;
			case 'edit':
				$countryId = intval($this->arrRequest[3]);
				$objCountry = new Country($countryId);
				if (!$objCountry->id) throw new Exception_SiteError('', 404);
				$this->actionEditCountry($objCountry);
				break;
			case 'article':
				switch ($this->arrRequest[3]) {
					case '': $this->actionArticleList(); break;
					case 'add': $this->actionAddArticle(); break;
					case 'edit':
						$articleId = intval($this->arrRequest[4]);
						$objArticle = new Article($articleId);
						if ($objArticle->id && $objArticle->getField('ID_essence_type') == Country::getEssenceType()) $this->actionEditArticle($objArticle);
						else throw new Exception_SiteError('', 404);
						break;
					default: throw new Exception_SiteError('', 404);
				}
				break;
			case 'gallery':
				$countryId = intval($this->arrRequest[3]);
				$objCountry = new Country($countryId);
				if (!$objCountry->id) throw new Exception_SiteError('', 404);
				$this->actionGallery($objCountry);
				break;
			default: throw new Exception_SiteError('', 404);
		}
	}
	
	/**
	 * Список всех городов
	 */
	private function actionCountriesList() {
		$objCountry = new Country();
		
		$arrCountries = $objCountry->find('all');
		
		$this->pageHead = 'Список стран (<a href="/admin/country/add">Добавить</a>)';
		$this->title = 'Список стран';
		
		$this->showHeader();
			View_Country::showAdminCountryList($arrCountries);
		$this->showFooter();
	}
	
	/**
	 * Страница добавления страны
	 */
	private function actionAddCountry() {
		if (isset($_POST['Country'])) {
			$objCountry = new Country($_POST['Country']);
			if ($objCountry->save()) User_Location::move('/admin/country');
			else print_r($objCountry->arrErrors);
		}
		else $objCountry = new Country();
		
		$this->pageHead = $this->title = 'Добавление страны';
		
		$this->showHeader();
			View_Country::showCountryForm($objCountry);
		$this->showFooter();
	}
	
	/**
	 * Страница редактирования страны
	 * @param Country $objCountry - объект страны
	 */
	private function actionEditCountry(Country $objCountry) {
		if (isset($_POST['Country'])) {
			$objCountry = new Country($_POST['Country']);
			if ($objCountry->save()) User_Location::move('/admin/country');
			else print_r($objCountry->arrErrors);
		}
		
		$this->pageHead = $this->title = 'Редактирование страны';
		
		$this->showHeader();
			View_Country::showCountryForm($objCountry);
		$this->showFooter();
	}
	
	private function actionArticleList() {
		$objArticle = new Article();
		
		$arrArticles = $objArticle->find('all', array(
			'conditions' => '`ID_essence_type` = ' . Country::getEssenceType(),
			'order' => '`created_at` DESC'
		));
		
		$this->title = 'Список статей стран';
		$this->pageHead = 'Список статей стран (<a href="' . W_ADMIN . '/country/article/add">Добавить</a>)';
		
		$this->showHeader();
			View_Article::showAdminArticleList($arrArticles, 'country');
		$this->showFooter();
	}
	
	private function actionAddArticle() {
		if (isset($_POST['Article'])) {
			$objArticle = new Article($_POST['Article']);
			if ($objArticle->save()) User_Location::move('/admin/country/article');
			else print_r($objArticle->arrErrors);
		}
		else $objArticle = new Article();
	
		$objCountry = new Country();
		$arrCountries = $objCountry->find('all', array(
			'select' => '`ID_country`, `country_name`',
			'order' => '`country_name` ASC'
		));
		$arrCountries = Service_ArrayConverter::makeSimpleArrayToSelect($arrCountries, 'ID_country', 'country_name');
		
		$arrCities = City::getCitiesForSelect();
		
		$this->title = $this->pageHead = 'Добавление статьи';
	
		$this->showHeader();
			View_Article::showArticleForm($objArticle, $objCountry, $arrCities, $arrCountries);
		$this->showFooter();
	}
	
	private function actionEditArticle(Article $objArticle) {
		if (isset($_POST['Article'])) {
			$objArticle = new Article($_POST['Article']);
			if ($objArticle->save()) User_Location::move('/admin/country/article');
			else print_r($objArticle->arrErrors);
		}
	
		$objCountry = new Country();
		$arrCountries = $objCountry->find('all', array(
			'select' => '`ID_country`, `country_name`',
			'order' => '`country_name` ASC'
		));
		$arrCountries = Service_ArrayConverter::makeSimpleArrayToSelect($arrCountries, 'ID_country', 'country_name');
	
		$arrCities = City::getCitiesForSelect();
	
		$this->title = $this->pageHead = 'Редактирование статьи';
	
		$this->showHeader();
			View_Article::showArticleForm($objArticle, $objCountry, $arrCities, $arrCountries);
		$this->showFooter();
	}
	
	/**
	 * Галерея страны
	 * @param Country $objCountry
	 */
	private function actionGallery(Country $objCountry) {
		if (!empty($_FILES)) {
			Photo::savePhotosFromFiles(Country::getEssenceType(), $objCountry->id);
			$objCountry = new Country($objCountry->id);
		}
		
		$arrPhotos = $objCountry->getPhotosIds();
		$this->title = $this->pageHead = $objCountry->getField('country_name') . ' - галерея';
		
		$this->showHeader();
			View_Photo::showPhotos($arrPhotos, $objCountry->getField('ID_photo'));
			View_Photo::showGalleryForm($objCountry);
		$this->showFooter();
	}
	
}